/******************************************************************************
 * (C) Copyright 
 * FILE NAME:    driver_watch_dog.c
 * DESCRIPTION:
 * 
 * DATE BEGUN:   
 * BY:          
 * PRODUCT NAME:
 * APPLICATION:
 * TARGET H/W:   MC9S12G128
 * DOC REF:
 *****************************************************************************
 */

/*****************************************************************************
** #include 
*****************************************************************************/
#include "system.h"
#include "drv_watch_dog.h"

/*****************************************************************************
** #define
*****************************************************************************/


/*****************************************************************************
** typedef
*****************************************************************************/


/*****************************************************************************
** global variable
*****************************************************************************/


/*****************************************************************************
** static variables
*****************************************************************************/


/*****************************************************************************
** static constants
*****************************************************************************/


/*****************************************************************************
** static function prototypes
*****************************************************************************/


/*****************************************************************************
** function prototypes
*****************************************************************************/


/****************************************************************************/
/****************************************************************************/
/**
 * Function Name: drv_watchDogInit
 * Description: 1. COP Clock select ACLK
 *              2. Stop COP and RTI to keep running in Active BDM mode
 *
 * Param:   none
 * Return:  TRUE  - Init Success
 *          FALSE - Init Filure
 * Author: 
 ****************************************************************************/
void drv_watchDogInit(void) 
{
	
#if (WATCH_DOG_ON == TRUE)  
	  
		/* COPCLK = ALCK = 10kHz
		** COP Watchdog Rates if COPOSCSEL1=1
		** CR2 CR1 CR0 Cycles to Time-out
		**	0	0	0	COP disabled
		**	0	0	1	  2^7		 12.8 ms
		**	0	1	0	  2^9		 51.2 ms
		**	0	1	1	  2^11		204.8 ms
		**	1	0	0	  2^13		819.2 ms
		**	1	0	1	  2^15	   3.2768 s
		**	1	1	0	  2^16	  13.1072 s
		**	1	1	1	  2^17	  52.4288 s
		*/
		//write to CPMUCOP before writing to COPOSCSEL1.
		// disable COP at BDM
		CPMUCOP =	CPMUCOP_RSBCK_MASK + WATCHDOG_TIMEOUT_VALUE;
					
					
		/* CPMUCLKS_COPOSCSEL1:
		** 0:COP clock source defined by COPOSCSEL0
		** 1:COP clock source is ACLK derived from a trimmable internal RC-Oscillator(10kHz)
		*/
		CPMUCLKS_COPOSCSEL1 = 1;
		
		/* CPMUCLKS_COPOSCSEL0:
		** 0:COP clock source is IRCCLK.
		** 1:COP clock source is OSCCLK
		*/ 
		CPMUCLKS_COPOSCSEL0 = 0; 
		
#else
		 _DISABLE_COP();
		  
#endif    
	
}


/****************************************************************************/
/**
 * Function Name: drv_watchDogClr
 * Description: Watch Dog reset count
 *
 * Param:   none
 * Return:  none
 * Author:  
 ****************************************************************************/
void drv_watchDogClr(void) 
{
	
#if (WATCH_DOG_ON == TRUE)
	
		_FEED_COP();
		
#else
	
#endif
	
}


/****************************************************************************/
/**
 * Function Name: drv_watchDogReset
 * Description: Reset MCU!!!
 *
 * Param:   none
 * Return:  none
 * Author:  
 ****************************************************************************/
void drv_watchDogReset( void )
{     
   (*(volatile unsigned char*)_COP_RST_ADR) = 0x00;
   (*(volatile unsigned char*)_COP_RST_ADR) = 0x00;
}

/*****************************************************************************/
/**
* Function Name: void drv_DisableWatchDog(void) 
* Description:   
* Param:         none
* Return:        none
* Author:  
******************************************************************************/
void drv_DisableWatchDog(void) 
{ 
#if (WATCH_DOG_ON == TRUE)
 
    _DISABLE_COP();
    
#else

#endif  

}


/*****************************************************************************
** End File
*****************************************************************************/

